package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.baseUtil.JWTUtils;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.entity.StudentPO;
import com.ajmd.onlineexam.entity.TeacherPO;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ajmd
 * @since 2023/3/30 21:43
 */
@Api("教师端端接口")
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    @ApiOperation("通过account查询教师")
    @GetMapping("/select")
    public CommonResult findStudentByAccount(@RequestParam String account){
        TeacherPO teacher = teacherService.findTeacherById(account);
        return CommonResult.ok().data("user",teacher);
    }
    @ApiOperation("查询所有学生")
    @PostMapping("/findPage")
    public CommonResult findAllStudent(@MultiRequestBody Integer userType,
                                       @MultiRequestBody Integer currentPage,
                                       @MultiRequestBody Integer pageSize,
                                       @MultiRequestBody SearchParam searchParam){
        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        Page<TeacherPO> pageData = teacherService.findAllStudent(currentPage,pageSize,searchParam);
        return CommonResult.ok().data("pageData",pageData);
    }

    @ApiOperation("教师登录")
    @PostMapping("/login")
    public CommonResult studentLogin(@RequestParam String account,
                                     @RequestParam String password){
        //登录数据校验
        if(StringUtils.isBlank(account)){
            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_NOT_EMPTY);
        }
        if(StringUtils.isBlank(password)){
            return CommonResult.error(AppExceptionCodeEnum.PASSWORD_NOT_EMPTY);
        }
        TeacherPO teacherPO = teacherService.findTeacherById(account);
        if(ObjectUtils.isEmpty(teacherPO)){
            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_NOT_EXISTS);
        }
        if(!teacherPO.getPassword().equals(password)){
            return CommonResult.error(AppExceptionCodeEnum.PASSWORD_ERROR);
        }
        Map<String, String> map = new HashMap<>();
        map.put("account",account);
        String token = JWTUtils.getToken(map);
        return CommonResult.ok("登录成功").data("token",token).data("user",teacherPO);
    }

//    @ApiOperation("教师注册")
//    @PostMapping("/register")
//    public CommonResult registerTeacher(@RequestBody TeacherPO teacher){
//        //数据校验
//        if(StringUtils.isBlank(teacher.getAccount())){
//            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_NOT_EMPTY);
//        }
//        if(StringUtils.isBlank(teacher.getPassword())){
//            return CommonResult.error(AppExceptionCodeEnum.PASSWORD_NOT_EMPTY);
//        }
//        TeacherPO teacherPO = teacherService.findTeacherById(teacher.getAccount());
//        if(ObjectUtils.isNotEmpty(teacherPO)){
//            return CommonResult.error(AppExceptionCodeEnum.ACCOUNT_EXISTS);
//        }
//        teacherService.teacherRegister(teacherPO);
//        return CommonResult.ok("注册成功");
//    }


    @ApiOperation("修改教师信息")
    @PutMapping("/update")
    public CommonResult updateTeacherByPO(@RequestBody TeacherPO teacher){
        int num = teacherService.teacherUpdate(teacher);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_ERR)
                    .appendMessage("未更新任何信息");
        }
        return CommonResult.ok("信息更新成功");
    }

    @ApiOperation("重置教师密码")
    @PutMapping("/resetPassword")
    public CommonResult updateStudentPassword(@RequestParam Integer userType,@RequestParam String account){
        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        int num = teacherService.resetPassword(account);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_ERR)
                    .appendMessage("重置密码失败");
        }
        return CommonResult.ok("重置密码成功");
    }

    @ApiOperation("删除教师信息")
    @DeleteMapping("/delete")
    public CommonResult deleteStudentById(@RequestParam Integer userType,@RequestParam String account){
        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        int num = teacherService.delete(account);
        if(num==0){
            return CommonResult.error().message("删除失败");
        }
        return CommonResult.ok().message("删除成功");
    }

}
